package com.s69.springbootredis.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Controller
public class NewsController {

    @Autowired
    RedisTemplate redisTemplate;

    @GetMapping("/detail/{id}")
    public String see(@PathVariable Integer id, Model model){
        redisTemplate.opsForHash().increment("news_"+id,"read",1);
        Map map=redisTemplate.opsForHash().entries("news_"+id);
        model.addAttribute(map);
        return "news"+id;
    }

    @ResponseBody
    @RequestMapping("/addZan/{id}/{opType}")
    public Long addZan(@PathVariable Integer id,@PathVariable Integer opType){
        Long countZan;
        if(opType==1){
            countZan=redisTemplate.opsForHash().increment("news_"+id,"countZan",1);
        }else{
            countZan=redisTemplate.opsForHash().increment("news_"+id,"countZan",-1);
        }
        return countZan;
    }
}
